﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using AmiangDAL;
using AmiangDTO;
using System.IO;

namespace AmiangBLL
{
    public class bllMatHang
    {
        public static BanHang2DataContext dbs = new BanHang2DataContext();

        public IQueryable GetDataTableNormally()
        {
            var data = from n in dbs.MATHANGs
                       select new
                       {
                           MaMH = n.MaMH,
                           TenMH = n.TenMH,
                           TenNSX = n.NHASANXUAT.TenNSX,
                           TenSIZE = n.SIZE.TenSIZE,
                           DonGiaBanLe = n.DonGiaBanLe,
                           DonGiaBanSi = n.DonGiaBanSi,
                           SoLuongTrongKho = n.SoLuongTrongKho,
                           SoLuongDaBan = n.SoLuongDaBan,
                           TinhTrang = n.TinhTrang,
                           MoTa = n.MoTa,
                           HinhAnh = n.HinhAnh
                       };
            return data;
        }


        public object GetValue(IQueryable condition)
        {
            object item = condition.Cast<object>().First();
            return item;
        }

        public bool AddRow(dtoMatHang dto, ref string error)
        {
            MATHANG dt = new MATHANG();
            bool success;
            dt.MaMH = dto.MaMH;
            dt.MaLMH = dto.MaLMH;
            dt.MaNSX = dto.MaNSX;
            dt.MaSIZE = dto.MaSIZE;
            dt.TenMH = dto.TenMH;
            dt.DonViTinh = dto.DonViTinh;
            dt.SoLuongDaBan = dto.SoLuongDaBan;
            dt.SoLuongTrongKho = dto.SoLuongTrongKho;
            dt.DonGiaBanLe = dto.DonGiaBanLe;
            dt.DonGiaBanSi = dto.DonGiaBanSi;
            dt.MoTa = dto.MoTa;
            dt.HinhAnh = dto.HinhAnh;
            dt.TinhTrang = dto.TinhTrang;
            try
            {

                dbs.MATHANGs.InsertOnSubmit(dt);
                dbs.SubmitChanges();
                success = true;
            }
            catch (Exception err)
            {
                error = err.Message;
                success = false;
            }
            return success;
        }

        public bool UpdateRow(dtoMatHang dto, ref string error)
        {
            bool success;
            MATHANG dt = (from n in dbs.MATHANGs
                          where n.MaMH == dto.MaMH
                          select n).Single();
            dt.MaLMH = dto.MaLMH;
            dt.MaNSX = dto.MaNSX;
            dt.MaSIZE = dto.MaSIZE;
            dt.TenMH = dto.TenMH;
            dt.DonViTinh = dto.DonViTinh;
            dt.SoLuongDaBan = dto.SoLuongDaBan;
            dt.SoLuongTrongKho = dto.SoLuongTrongKho;
            dt.DonGiaBanLe = dto.DonGiaBanLe;
            dt.DonGiaBanSi = dto.DonGiaBanSi;
            dt.MoTa = dto.MoTa;
            dt.TinhTrang = dto.TinhTrang;
            try
            {

                dbs.SubmitChanges();
                success = true;
            }
            catch (Exception err)
            {
                error = err.Message;
                success = false;
            }
            return success;
        }


        public bool DeleteRow(dtoMatHang dto, ref string error)
        {
            bool success;
            MATHANG item = (from n in dbs.MATHANGs
                            where n.MaMH == dto.MaMH
                            select n).Single();
            try
            {
                dbs.MATHANGs.DeleteOnSubmit(item);
                dbs.SubmitChanges();
                success = true;
            }
            catch (Exception err)
            {
                error = err.Message;
                success = false;

            }
            return success;
        }
        public MemoryStream LoadImage(string MaMH)
        {
            MATHANG mathang = (from n in dbs.MATHANGs
                               where n.MaMH.Contains(MaMH)
                               select n).First();
            MemoryStream stream = new MemoryStream(mathang.HinhAnh.ToArray());
            return stream;
        }
    }
}
